// CommandLineHelper Library
// =========================
// License: Microsoft Permissive License (Ms-PL) v1.1
// Author: Rhy Mednick (http://www.rhyverse.net)
// Project Site: http://www.codeplex.com/CommandLineHelper


using System;
using System.Collections.Generic;
using System.Text;

namespace CommandLineHelper
{
    internal static class WrapConsole
    {
        /// <summary>
        /// Write a string of text to the console, wrapping on word breaks, and indenting the specified amount.
        /// </summary>
        /// <param name="indent">The amount of spaces to indent the wrapped text.</param>
        /// <param name="text">The text to write.</param>
        public static void Write(int indent, string text)
        {
            int startPos = Console.CursorLeft;
            int consolePos = startPos;

            if (Console.BufferWidth <= indent)
            {
                throw new ArgumentException("Console buffer width is less than or equal to the indent parameter value, so there is no place to write the text.");
            }
            string[] words = text.Split(' ');
            for (int i = 0; i < words.Length; i++)
            {
                bool manualWrap = false;
                if ((Console.CursorLeft + words[i].Length) > (Console.BufferWidth - 1))
                {
                    //wrap the text before writing the word.
                    Console.WriteLine();
                    //and indent
                    Console.Write(new String(' ', indent));
                    manualWrap = true;
                }
                if (manualWrap)
                {
                    //we've wrapped manually, so confirm that the next word will actually fit on a line by itself.  If it won't, then break it up.
                    if (words[i].Length > (Console.BufferWidth - indent))
                    {
                        int lineLength = Console.BufferWidth - indent;
                        int segments = (words[i].Length / lineLength) + 1;

                        for (int j = 0; j < segments; j++)
                        {
                            if (j == (segments - 1))
                            {
                                Console.Write("{0} ", words[i].Substring(lineLength * j));
                            }
                            else
                            {
                                Console.Write(words[i].Substring(lineLength * j, lineLength));
                                Console.Write(new string(' ', indent));
                            }

                        }
                    }
                    else
                    {
                        Console.Write("{0} ", words[i]);
                    }
                }
                else
                {
                    Console.Write("{0} ", words[i]);
                }

                //if we're at the start of a new line after writing, then we auto-wrapped, so indent 
                if (Console.CursorLeft == 0)
                {
                    Console.Write(new String(' ', indent));
                }


            }
        }

        public static void WriteLine(int indent, string text)
        {
            WrapConsole.Write(indent, text);
            Console.WriteLine();
        }
    }
}
